package com.sas.entity.converter;

import javax.persistence.AttributeConverter;
import javax.persistence.Converter;

/**
 * 将数据库中的员工类型字符串枚举与实体中的整型值进行互相转换。
 * DB: 'FULL_TIME' | 'PART_TIME' | 'TEMPORARY'
 * Entity: 1 | 2 | 3
 */
@Converter(autoApply = false)
public class EmployeeTypeConverter implements AttributeConverter<Integer, String> {

    @Override
    public String convertToDatabaseColumn(Integer attribute) {
        if (attribute == null) return null;
        switch (attribute) {
            case 1:
                return "FULL_TIME";
            case 2:
                return "PART_TIME";
            case 3:
                return "TEMPORARY";
            default:
                // 回退到临时工，避免未知数值写入异常
                return "TEMPORARY";
        }
    }

    @Override
    public Integer convertToEntityAttribute(String dbData) {
        if (dbData == null) return null;
        switch (dbData) {
            case "FULL_TIME":
                return 1;
            case "PART_TIME":
                return 2;
            case "TEMPORARY":
                return 3;
            default:
                try {
                    // 兼容潜在的数字存储
                    return Integer.parseInt(dbData);
                } catch (NumberFormatException ex) {
                    // 默认回退为临时工
                    return 3;
                }
        }
    }
}